<template>
  <ui-page class="container">
    <!-- table -->
    <ui-table v-bind="table.config" ref="table" class="content_container" :params="{tenantId: userInfo.tenantId}"
              @select-change="onSelectTableEntity">
      <!--   查询   -->
      <template v-slot:search-bar>
        <ui-table-select v-model="table.model.select" :reset="true">
          <ui-table-select-item name="employeeId" label="员工编号" placeholder="请输入员工编号" show-label type="number"
                                start-null/>
          <ui-table-select-item name="name" label="姓名" placeholder="请输入姓名" show-label type="text"
                                :show-word-limit="false"/>
          <ui-table-select-item name="departmentId" label="部门" placeholder="请输入部门" show-label type="select"
                                start-null :data="departmentList" :props="{value: 'id', label: 'name'}"/>
          <ui-table-select-item name="exceptionRecord" label="异常记录" placeholder="请输入异常记录" show-label type="select"
                                :show-word-limit="false" :data="abnormalRecords"/>
          <ui-table-select-item name="cardReplacementTime" label="补卡时间" placeholder="请选择补卡时间" show-label type="datetimerange"
                                start-null/>
          <ui-table-select-item name="status" label="审批状态" placeholder="请选择审批状态"
                                show-label type="select" :data="approvalStatus" start-null/>
        </ui-table-select>
      </template>
      <ui-table-column label="序号" type="index" width="60px"/>
      <ui-table-column prop="employeeId" label="员工编号"/>
      <ui-table-column prop="entName" label="姓名"/>
      <ui-table-column prop="depName" label="部门"/>
      <ui-table-column prop="exceptionRecordText" label="异常记录"/>
      <ui-table-column prop="exceptionDate" label="异常日期"/>
      <ui-table-column prop="cardReplacementTime" label="补卡时间"/>
      <ui-table-column prop="cardReplacementReason" label="补卡事由"/>
      <ui-table-column prop="supervisorName" label="主管审批"/>
      <ui-table-column prop="statusText" label="审批状态">
        <template v-slot:default="{row}">
          <span :style="{color: row.statusText === '未审批' ? 'blue' : (row.statusText === '已审批' ? 'green' : 'red')}">
            {{ row.statusText }}
          </span>
        </template>
      </ui-table-column>
      <!--   分页   -->
      <template v-slot:footer-bar>
        <ui-table-pagination/>
      </template>
    </ui-table>
  </ui-page>
</template>
<script>
import {mapState} from 'vuex';
import to from "await-to-js";

const CardReplacementRecord = {
  API: {
    create: '/Card/CardReplacementRecord/create',
    update: '/Card/CardReplacementRecord/update',
    delete: '/Card/CardReplacementRecord/delete',
    select: '/Card/CardReplacementRecord/select',
    export: '/Card/CardReplacementRecord/export',
    createList: '/Card/CardReplacementRecord/createList',
    updateList: '/Card/CardReplacementRecord/updateList',
    deleteList: '/Card/CardReplacementRecord/deleteList',
    selectList: '/Card/CardReplacementRecord/selectList',
    selectPage: '/Card/CardReplacementRecord/selectPage',
  },
  data() {
    return {
      api: CardReplacementRecord.API,
      table: {
        config: {
          url: CardReplacementRecord.API.selectPage,
        },
        model: {
          select: {},
        },
      },
      form: {
        isCreate: true,
        title: '新增',
        url: CardReplacementRecord.API.create,
        visible: false,
        detailVisible: false,
        rules: {},
      },
      departmentList: [],
      detailList: {},
      abnormalRecords: [{value: 0, label: '上班异常'},{value: 1, label: '下班异常'}],
      approvalStatus: [{value: 0, label: '未审批'}, {value: 1, label: '已审批'}, {value: 2, label: '已拒绝'}]
    };
  },
  computed: {
    ...mapState(['userInfo']),
  },
  mounted() {
    this.getDepartment()
  },
  methods: {
    onSelectTableEntity(selects) {
      if (selects.length <= 0) {
        return;
      }
      this.handleEntityUpdateRow(selects[selects.length - 1]);
    },
    handleEntityCreate() {
      this.form.isCreate = true;
      this.$refs.form.resetValues();
      this.form.title = '新增';
      this.form.url = this.api.create;
      this.form.visible = true;
    },
    handleEntityUpdate() {
      const sels = this.$refs.table.getSelection();
      if (sels.length !== 1) {
        this.$message.info('请选择要修改的数据');
        return;
      }
      this.handleEntityUpdateRow(sels[0]);
    },
    handleEntityDelete() {
      const sels = this.$refs.table.getSelection();
      if (sels.length <= 0) {
        this.$message.info('请选择要删除的数据');
        return;
      }
      this.deleteEntities(sels.map(item => item.id)).then(({success}) => {
        if (!success) {
          return;
        }
        this.reloadTable();
      });
    },
    handleEntityUpdateRow(row) {
      this.form.isCreate = false;
      this.form.title = '修改';
      this.form.url = this.api.update;
      this.form.visible = true;

      this.selectEntity(row.id).then(({success, data}) => {
        if (!success) {
          this.form.visible = false;
          return;
        }
        console.log(data)
        this.$refs.form.resetValues(data);
      });
    },
    handleEntityDeleteRow(row) {
      this.deleteEntity(row.id).then(({success}) => {
        if (!success) {
          return;
        }
        this.reloadTable();
      });
    },
    async handleEntityExport() {
      const loading = this.$loading({text: '数据加载中..'});
      const url = this.api.export;
      await this.$refs.table.exportEntity(url, '导出.xls');
      loading.close();
    },
    reloadTable() {
      this.$refs.table && this.$refs.table.reload();
    },
    afterFormSubmit({success}) {
      if (!success) {
        return;
      }
      this.form.visible = false;
      this.reloadTable();
    },
    async getDepartment () {
      const tenantId = this.userInfo.tenantId
      const [, result] = await to(this.$axios.get(`/Ent/EntDepartment/selectList?tenantId=${tenantId}`))
      this.departmentList = result.data || []
      console.log(this.departmentList)
    },
  },
};
export default CardReplacementRecord;
</script>
<style lang="less" scoped>
</style>
